import java.util.HashSet;
import java.util.PriorityQueue;
import java.util.Set;

public class StaticCanCommunicationService {
    public static void main(String[] args) {
        int[][] test1= {{1,0},{0,1}};
        int[][] test2= {{1,0},{1,1}};
        int[][] test3= {{1,1,0,0},{0,0,1,0},{0,0,1,0},{0,0,0,1}};
        int[][] test4= {{1,0,0,1,0},{0,0,0,0,0},{0,0,0,1,0}};
        int[][] test5= {{0,0,0,0},{1,1,1,1},{0,0,0,1},{0,0,1,1},{0,0,0,1}};
        int[][] test6= {{0,0,1,0,1},{0,1,0,1,0},{0,1,1,1,0},{1,0,0,1,1},{0,0,1,1,0}};
        StaticCanCommunicationService staticCanCommunicationService = new StaticCanCommunicationService();
        System.out.println(staticCanCommunicationService.countServers(test1));
        System.out.println(staticCanCommunicationService.countServers(test2));
        System.out.println(staticCanCommunicationService.countServers(test3));
        System.out.println(staticCanCommunicationService.countServers(test4));
        System.out.println(staticCanCommunicationService.countServers(test5));
        System.out.println(staticCanCommunicationService.countServers(test6));
    }
    public int countServers(int[][] grid) {
        int result = 0;
        int m = grid.length;
        int n = grid[0].length;
        int[] mArr = new int[m];
        int[] nArr = new int[n];

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if(grid[i][j] == 1){
                    mArr[i] = mArr[i] +1;
                    nArr[j] = nArr[j] +1;
                }
            }
        }


        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if(grid[i][j] == 1 && (mArr[i]>1 ||nArr[j]>1 )){
                    result++;
                }
            }
        }


        
        
        return result;
    }

}
